package com.room.controller.college;

import com.room.pojo.College;
import com.room.pojo.DormResult;
import com.room.pojo.Student;
import com.room.pojo.vo.CollegeVO;
import com.room.pojo.vo.StudentVO;
import com.room.service.CollegeService;
import com.room.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 lin
 * @创建时间 2019/10/6
 * @描述
 */
@RestController
@RequestMapping("/college/manager")
public class CollegeController {

    @Autowired
    CollegeService collegeService;

    @RequestMapping(value = "/col",method = RequestMethod.POST)
   public DormResult insertCollege(College college){
     int s =   Integer.parseInt(collegeService.getMaxId())+1;
     String no = String.valueOf(s);
     college.setCollegeId(no);
     int i = collegeService.insertCollege(college);
     if(i==1){
         return DormResult.ok("添加新学院成功");
     }
       return DormResult.error("添加新学院失败");
   }
    /**
     * 删除指定id的学院
     * @param ids
     * @return
     */
    @RequestMapping(value = "/col/{ids}",method = RequestMethod.DELETE)
    public DormResult deleteById(@PathVariable String ids){
        if(collegeService.deleteColById(ids)){
            return DormResult.ok("删除成功");
        }
        return DormResult.error("删除失败");
    }
    /**
     * 获取学院数据
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/col",method = RequestMethod.GET)
    public Map<String,Object> getCollegeByPage(
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "6")Integer size
    ){

        Map<String,Object> map = new HashMap<String,Object>();
        List<College> colleges = collegeService.getCollegeByPage(page,size);
        Long total = collegeService.getCollegeCount();
        map.put("colleges",colleges);
        map.put("total",total);
        return map;
    }

    /**
     * 获取数据用来初始化前端的下来列表
     * @return
     */
    @RequestMapping(value="/getInitData")
    public List<College> getAllCollege(){
        return collegeService.getAllCollege();
    }

    @RequestMapping(value = "exportCol",method = RequestMethod.GET)
    public void toExport(HttpServletResponse response) {
        List<CollegeVO> collegeVOS = collegeService.getCollegeVO();
        FileUtils.exportExcel(collegeVOS, "学院信息信息表 title" , "学院信息信息表 sheet", CollegeVO.class, "学院信息公寓表.xls", response);
    }
}
